package com.glsc.ngateway.opmanage.service.deposit;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsAccBankInfo;
import com.glsc.ngateway.common.base.domain.oracle.xinyizj.FsInDeposMarginBal;
import com.glsc.ngateway.common.base.repo.oracle.xinyizj.FsInDeposMarginBalRepository;
import com.glsc.ngateway.opmanage.utils.DateUtils;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 柜台余额
 */
@Service
public class FsInDeposMarginBalService {


    @Resource
    private FsInDeposMarginBalRepository fsInDeposMarginBalRepository;

    @Resource
    private FsAccBankInfoService fsAccBankInfoService;

    /**
     * 查询客户保证金-柜台余额
     *
     * @param bankAcc
     * @return
     */
    public BigDecimal findMarginBalByBankAcc(String bankAcc) {
        List<FsInDeposMarginBal> fsInDeposMarginBals = listByPayBankAcc(bankAcc, Long.parseLong(DateUtils.getNowDateString().replaceAll("-", "")), "0");
        if (CollectionUtils.isEmpty(fsInDeposMarginBals)) {
            return BigDecimal.ZERO;
        }

        BigDecimal res = BigDecimal.ZERO;
        for (FsInDeposMarginBal fsInDeposMarginBal : fsInDeposMarginBals) {
            if (Objects.nonNull(fsInDeposMarginBal.getMarginBal())) {
                res = res.add(fsInDeposMarginBal.getMarginBal());
            }
        }

        return res;
    }

    /**
     * 根据账号查询信息
     *
     * @param bankAcc
     * @return
     */
    public List<FsInDeposMarginBal> listByPayBankAcc(String bankAcc, Long balDate, String fundProp) {

        FsAccBankInfo accBankInfo = fsAccBankInfoService.findByBankAcc(bankAcc);

        List<FsInDeposMarginBal> list = fsInDeposMarginBalRepository.findAll(Example.of(FsInDeposMarginBal.builder()
                .bankCode(accBankInfo.getBankCode()).balDate(balDate).fundProp(fundProp).build()));
        return list;
    }

    /**
     * /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<FsInDeposMarginBal> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "bankAcc";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<FsInDeposMarginBal> page = fsInDeposMarginBalRepository.findAll(SpecificationUtil.buildSpecification(param, FsInDeposMarginBal.class), pageable);

        return page;
    }

    /**
     * 查询所有
     *
     * @return
     */
    public List<FsInDeposMarginBal> listAll() {
        List<FsInDeposMarginBal> list = fsInDeposMarginBalRepository.findAll(Example.of(FsInDeposMarginBal.builder().build()));
        return list;
    }


}
